export default function({tapDay, today, funButtons, record, caseKey}){
  const $$ = wx.calendarInst.getDateHandle
  const that = this
  const key = caseKey
  funButtons.dot.unshift({
    aim: 'onRemind',
    itemClass: 'remind-case', 
    img: {src: '/images/remind.png'}
  })
  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key+'&type=reciprocal'
    }
    return item
  })
  // if (record) {
  //   wx.showToast({
  //     title: '重复设置',
  //     icon: 'error',
  //     duration: 2000
  //   })
  //   return
  // }

  const daystamp = 24 * 60 * 60 * 1000
  const remainder = (tapDay.timestamp - today.timestamp) % daystamp
  const diffDays = (tapDay.timestamp - today.timestamp) / daystamp + remainder
  tapDay.diffDays = diffDays

  const title = `${tapDay.month}月${tapDay.day}日`
  const content = {body: [`${diffDays}`, title], date: tapDay.timestr, itemClass: 'reciprocal', targetDay: tapDay, ...funButtons}

  function beforeCallback({createCaseCard}){
    wx.modalx.showModal({
      content: {
        // title: ['倒数', diffDays+'', '天'],
        title: [' ', '🕟', '倒数'+diffDays+'天'],
        body: [
          {
            title: [
              '倒数原因：',
              {
                $$id: 'reciprocal-reason-title',
                title: ' '
              }
            ],
            titleStyle: 'display: grid; grid-template-columns: 1fr 2.5fr; grid-template-rows: 1fr;box-sizing: border-box; padding-left: 0.3em;',
            "@list":{
              type: {
                is: 'scroll',
                "scroll-y": true,
                "scroll-x": false,
              },
              data: [
                '自定义',
                '生日',
                '爸爸的生日',
                '妈妈的生日',
                '儿子生日',
                '女儿生日',
                '女朋友生日',
                // {title: '双11', date: '11-11'}
              ],
              itemMethod: {
                tap(e, param, inst){
                  inst.siblings().removeClass('active', function(){
                    inst.addClass('active')
                    const titleInst = that.getElementsById('reciprocal-reason-title')
                    const data = inst.getData()
                    const title = data.title
                    if (titleInst) {
                      if (title === '自定义') {
                        titleInst.update({title: '自定义，请在卡片中编辑'})
                        content.body[2] = '即将到来的日子'
                      } else {
                        titleInst.update({title})
                        content.body[2] = title
                      }
                    }
                  })
                }
              },
              listClass: 'reciprocal-reason-list',
              itemClass: 'reciprocal-reason-item',
            },
            dot: [
              {
                title: '我的倒数日', 
                itemClass: 'pop-case-scroll-bottom', 
                itemStyle: 'margin-top: 0; margin-bottom: 1em;',
                aim(){
                  wx.modalx.closeModal()
                  that.restoreCaseButton()
                  wx.pageScrollTo({
                    selector: '#pits-list-container',
                    duration: 300,
                    success(){
                      that.listCaseType('reciprocal')
                    }
                  })
                }
              }, 
            ]
          },
        ],
        itemClass: 'inner-content',
      },
      confirmText: '确定',
      cancelText: '取消', 
      height: 65,
      width: 80,
      itemClass: 'popup reciprocal', 
      mask: false,
      success(res){
        if (res.confirm) {
          if (tapDay.timestamp <= today.timestamp) {
            wx.showToast({
              title: '不能小于明天',
              icon: 'error',
              duration: 2000
            })
            return
          }
          const timestr = tapDay.timestr
          const currentDay = $$(timestr)
          currentDay && currentDay.addClass('reciprocal')
          
          createCaseCard(content)
        }
        that.restoreCaseButton()
      }
    })
  }

  return {key, content, beforeCallback}
}